package com.zhht.checkcop.demo;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import com.zhht.checkcop.db.MysqlDb;
import com.zhht.checkcop.db.StaticDBMap;
import com.zhht.checkcop.parser.ParserMovieInfoByUpdateLXJ;

public class MovieUpdateLXJ implements PageProcessor{
	private final static Logger log= Logger.getLogger(MovieUpdateLXJ.class);
	
	private Site site = Site.me().setCycleRetryTimes(3).setSleepTime(1500).setTimeOut(5000);
	private List<String> novelListUrl;
	public MovieUpdateLXJ(){}
	public MovieUpdateLXJ(List<String> novelListUrl){
		this.novelListUrl=novelListUrl;
	}
	
	private Boolean bool=true;
	
	public void process(Page page) {
    	try{
    		if(bool){
    			bool=false;
    			page.addTargetRequests(novelListUrl);
    		}else{
    			
		    	page.putField("infoUrl", page.getRequest().getUrl());
		    	List<String> downUrl = page.getHtml().xpath("//a/text()").all();
		    	List<String> addUrl=new ArrayList<String>();
		    	if(downUrl!=null&&downUrl.size()>0){
		    		for(String strUrl:downUrl){
		    			if(strUrl.indexOf("magnet")>-1||strUrl.indexOf("btbo")>-1||strUrl.indexOf("ftp")>-1){
		    				addUrl.add(strUrl);
		    			}
		    		}
		    	}
		    	page.putField("downUrl", addUrl);
    		}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }

    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
//    	SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
		SynchronizeNewNovel(args);
//		Mail.send("时间："+sdf.format(new Date())+"  验证 sql:select count(1) from novel_info where source = 1 and thisSyn = 0 and d = 0  失败同步书籍数量:");
    }
	private static void SynchronizeNewNovel(String[] args) {
		try{
        	MysqlDb mysqldb = new MysqlDb();
        	mysqldb.connect();
        	
        	StaticDBMap.existNovelNameMap = mysqldb.query("select id,infoUrl from movie_info where d = 0 and classifyId = 10",new ArrayList(),"infoUrl");
        	
        	int thread = args!=null&&args.length>0?Integer.parseInt(args[0]):1;
        	List<String> strList10 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 10 and d = 0 ");
        	mysqldb.close();
        	
        	Spider.create(new MovieUpdateLXJ(strList10)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfoByUpdateLXJ("10")).thread(thread).run();
        	log.info("连续剧已完成");
    	}catch (Exception e) {
			e.printStackTrace();
		}
	}
}
